package Engine.Manager
{
	import Engine.Model.EventManager;
	import Engine.Model.JsConnection;
	import Engine.Model.KeyEventMannager;
	import Engine.Model.LoaderManager;
	import Engine.Model.LogPrint;
	import Engine.Model.ResManager;
	import Engine.Model.SocketManager;
	
	import Interface.Common.IEngineManager;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.ui.Keyboard;
	import flash.utils.setTimeout;
	
	public class EngineManager implements IEngineManager
	{
		private static var _eventMgr:EventManager;
		private static var _socketMgr:SocketManager;
		private static var _logPrint:LogPrint;
		private static var _regMgr:ResManager;
		private static var _loaderMgr:LoaderManager;
		private static var _keyEvengMgr:KeyEventMannager;
		private static var _jsConnection:JsConnection;
		
		
		private var _loaderCallback:int;
		private var _stage:Stage;
		
		public function EngineManager()
		{
			this.initManager();
		}
		
		private function initManager():void
		{
			_logPrint = LogPrint.instance();
			_eventMgr = new EventManager();
			_regMgr = new ResManager();
			_loaderMgr = new LoaderManager();
			_keyEvengMgr = new KeyEventMannager();
			_jsConnection = new JsConnection();
			_socketMgr = new SocketManager();
			_loaderCallback = 0;
		}
		
		public static function get eventMgr():EventManager
		{
			return _eventMgr;
		}
		
		public static function get logPrint():LogPrint
		{
			return _logPrint;
		}
		
		public static function get socketMgr():SocketManager
		{
			return _socketMgr;
		}
		
		public function setStage(stage:Stage):void
		{
			this._stage = stage;
			_keyEvengMgr.setStage(stage);
			regestKeyUpEvent(Keyboard.F9,_logPrint.showOrHide);
		}
		
		public function regestKeyUpEvent(key:int,fun:Function):void
		{
			_keyEvengMgr.regestKeyUpEvent(key,fun);
		}
		
		public function destroyKeyUpEvent(key:int):void
		{
			_keyEvengMgr.destroyKeyUpEvent(key);
		}
		
		public function regestKeyDownEvent(key:int,fun:Function):void
		{
			_keyEvengMgr.regestKeyDownEvent(key,fun);
		}
		
		public function destroyKeyDownEvent(key:int):void
		{
			_keyEvengMgr.destroyKeyDownEvent(key);
		}
		
		
		public function addLogDebug(disparent:DisplayObjectContainer):void
		{
			disparent.addChild(_logPrint);
		}
		
		public function registerEvent(eventID:int,fun:Function) : void
		{
			_eventMgr.registerEvent(eventID,fun);
		}
		
		public function destroyEvent(eventID:int):void
		{
			_eventMgr.destroyEvent(eventID);
		}
		public function throwEvent(eventID:int,arg:Object = null):void
		{
			_eventMgr.throwEvent(eventID,arg);
		}
		
		public function sendMessageData(msgType:int,msgData:*):void
		{
			_socketMgr.sendMessageData(msgType,msgData);
		}
		
		public function connectSocket(ip:String,port:int):void
		{
			_socketMgr.connectSocket(ip,port);
		}
		
		public function setConnectFun(fun:Function):void
		{
			_socketMgr.setConnectFun(fun);
		}
		
		public function setDisConnectFun(fun:Function):void
		{
			_socketMgr.setDisConnectFun(fun);
		}
		
		public function disconnected():void
		{
			_socketMgr.disconnected();
		}
		
		public function printLog(...msg):void
		{
			_logPrint.printLog(msg.join(" "));
		}
		
		public 	function loadURLResVersion(version:String, onLoadComplete:Function):void
		{
			_regMgr.loadURLResVersion(version,onLoadComplete);
		}
		
		public function checkSharedObjectSaveSetting(onCompleteFunc:Function):void
		{
			_regMgr.checkSharedObjectSaveSetting(onCompleteFunc);
		}
		public function loadText(path:String, onCompleteFunc:Function, onProgressFunc:Function=null, onIOErrorFunc:Function=null, onSecurityErrorFunc:Function=null):void
		{
			_loaderMgr.loadText(path,onCompleteFunc,onProgressFunc,onIOErrorFunc,onSecurityErrorFunc);
		}
		public function loadSWF(path:String, onCompleteFunc:Function, onProgressFunc:Function=null, onIOErrorFunc:Function=null, onSecurityErrorFunc:Function=null):void
		{
			_loaderMgr.loadSWF(path,onCompleteFunc,onProgressFunc,onIOErrorFunc,onSecurityErrorFunc);
		}
		public function loadImage(path:String, onCompleteFunc:Function, onProgressFunc:Function=null, onIOErrorFunc:Function=null, onSecurityErrorFunc:Function=null):void
		{
			_loaderMgr.loadImage(path, onCompleteFunc, onProgressFunc, onIOErrorFunc, onSecurityErrorFunc);
		}
		
		public function startloading():void
		{
			if(this._loaderCallback)
				return;
			this._loaderCallback = setTimeout(this._startloading,100);
		}
		
		private function _startloading():void
		{
			this._loaderCallback = 0;
			_loaderMgr.startloading();
		}
		
		public function addJSCallFunction(funcName:String,funcAddr:Function):void
		{
			_jsConnection.addJSCallFunction(funcName,funcAddr);
		}
		
		
		public function callJSFunction(funcName:String):Object
		{
			return _jsConnection.callJSFunction(funcName);
		}
		
	}
}